package com.yootk.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

@WebServlet("/JSTLServlet5")
public class JSTLServlet5 extends HttpServlet {
    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private static final ZoneId ZONE_ID = ZoneId.systemDefault();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String str = "1998-02-17 19:09:15"; // 随意定义一个日期
        LocalDateTime localDateTime = LocalDateTime.parse(str, FORMATTER);
        Instant instant = localDateTime.atZone(ZONE_ID).toInstant();
        Date birthday = Date.from(instant); // 字符串转为日期型
        req.setAttribute("birthday", birthday);
        req.setAttribute("salary", 2838383);
        req.getRequestDispatcher("/2/test06.jsp").forward(req, resp); // 服务端跳转
    }
}
